﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using MS.BasicData.Domain;
using MS.BasicData.Domain.Role_Based_Access_Control;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;
using System;
using System.ComponentModel.DataAnnotations;
using System.Transactions;

namespace MS.BasicData.Read.API.Applications.UserInfos
{
    public class RegisterRequestHandler : IRequestHandler<RegisterRequestModel, ApiRestult>
    {
        private readonly IUserBaseRepository _userBaseRepository;

        private readonly IRoleBaseRepository _roleBaseRepository;

        private readonly IUserRoleBaseRepository _userRoleBaseRepository;

        private readonly IMapper _mapper;

        private readonly UserDataDbconterxt _db;

        public RegisterRequestHandler(IUserBaseRepository userBaseRepository, IRoleBaseRepository roleBaseRepository, IUserRoleBaseRepository userRoleBaseRepository)
        {
            _userBaseRepository = userBaseRepository;
            _roleBaseRepository = roleBaseRepository;
            _userRoleBaseRepository = userRoleBaseRepository;
        }

        public async Task<ApiRestult> Handle(RegisterRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult result = new ApiRestult
            {
                isSuc = true,
                Code = Restult.Success,
                Message = "注册成功"
            };

            try
            {
                //using (TransactionScope scope = new TransactionScope())
                using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    //创建用户
                    UserInfo userInfo = new UserInfo()
                {
                    UserName = request.UserName,
                    UserNick = request.UserNick,
                    Password = request.Password,
                    ErrNum = 0,
                    IsSuc = false
                };
                //添加到数据库
                await _userBaseRepository.InsertAsync(userInfo);

                //创建角色
                foreach (var item in request.RoleIds)
                {
                    //用户角色关系
                    UserRole userRole = new UserRole
                    {
                        UserInfoId = userInfo.UserInfoId,
                        RoleId = item
                    };
                    //添加到数据库
                    await _userRoleBaseRepository.InsertAsync(userRole);
                }

                //提交事务
                scope.Complete();
                }
                return result;
        }

            catch (Exception ex)
            {
                result.Code = Restult.Fail;
                result.Message = "注册失败";
                return result;
            }
        }
        }


    public class RegisterRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string? UserName { get; set; }
        /// <summary>
        /// 用户昵称
        /// </summary>
        public string? UserNick { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string? Password { get; set; }
        ///// <summary>
        ///// 错误次数
        ///// </summary>
        //public int ErrNum { get; set; }
        ///// <summary>
        ///// 用户状态
        ///// </summary>
        //public bool IsSuc { get; set; }
        //public int CreateOperatorId { get; set; }
        //public string? CreateOperatorName { get; set; }
        //public DateTime CreateDate { get; set; }

        //public bool IsDeleted { get; set; }
        /// <summary>
        /// 角色ID
        /// </summary>
        public List<int> RoleIds { get; set; }
    }

}
